﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ArmaModLauncher
{
    [Serializable]
    class Settings : ISerializable
    {
        public string ModFolder { get; set; }
        public string SteamFolder { get; set; }
        public CheckState CheckAllState { get; set; }
        public LaunchParameterSettings ParameterSettings { get; set; }

        public Settings()
        {
            ModFolder = ".";
            SteamFolder = ".";
            CheckAllState = CheckState.Unchecked;
        }

        public Settings(SerializationInfo info, StreamingContext context)
        {
            ModFolder = info.GetString("ModFolder");
            SteamFolder = info.GetString("SteamFolder");
            CheckAllState = (CheckState) info.GetValue("CheckAllState", typeof (CheckState));
            ParameterSettings =
                (LaunchParameterSettings) info.GetValue("ParameterSettings", typeof (LaunchParameterSettings));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("ModFolder", ModFolder);
            info.AddValue("SteamFolder", SteamFolder);
            info.AddValue("CheckAllState", CheckAllState);
            info.AddValue("ParameterSettings", ParameterSettings);
        }

        public void Serialize(string filename)
        {
            using (FileStream file = File.Open(filename, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(file, this);
            }
        }

        public static Settings Deserialize(string filename)
        {
            using (FileStream file = File.Open(filename, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                return (Settings) formatter.Deserialize(file);
            }
        }
    }
}
